//
//  StatusChangedAnimation.m
//  Statz
//
//  Created by Dave MacLachlan on 2007/11/27.
//  Copyright 2007 Google Inc. All rights reserved.
// 
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License.  You may obtain a copy
// of the License at
// 
// http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
// License for the specific language governing permissions and limitations under
// the License.
//


#import "StatusChangedAnimation.h"
#import "StatzProtocols.h"
#import "StatusItemView.h"

@implementation StatusChangedAnimation
- (id)initWithImage:(NSImage*)image statusItem:(NSStatusItem*)item {
  if ((self = [super initWithDuration:1.0 animationCurve:NSAnimationEaseInOut])) {
    icon_ = [image retain];
    item_ = [item retain];
    [self setAnimationBlockingMode:NSAnimationNonblocking];
  }
  return self;
}

- (void)dealloc {
  [item_ release];
  [icon_ release];
  [super dealloc];
}

- (void)setCurrentProgress:(NSAnimationProgress)progress {
  [super setCurrentProgress:progress];
  NSSize faderSize = NSMakeSize(16, 16);
  NSImage *fader = [[[NSImage alloc] initWithSize:faderSize] autorelease];
  if (progress > 0.5f) {
    progress = 1.0f - ((progress - 0.5f) * 2.0f);
  } else {
    progress = progress * 2.0f;
  }
  NSRect newRect = NSMakeRect(0, 0,faderSize.width, faderSize.height);
  NSSize size = [icon_ size];
  NSRect fromRect = NSMakeRect(0, 0, size.width, size.height);
  [fader lockFocus];
  [icon_ drawInRect:newRect fromRect:fromRect operation:NSCompositeSourceOver fraction:progress];
  [fader unlockFocus];
  StatusItemView *view = (StatusItemView*)[item_ view];
  [view setImage:fader];
}
@end
